﻿$OutputEncoding = [System.Text.Encoding]::utf8
function InitRootFile($CopyDir,$TargetDir) {
    <# 清空项目目录 只保留 node_modules 文件夹 #>
    $RootDirList=Get-ChildItem -Path $TargetDir -Recurse -Directory -Depth 0
    $RootFileList=Get-ChildItem -Path $TargetDir -Recurse -File -Depth 0
    if($global:AddMode -eq $false){
        foreach($Dir in $RootDirList){
            Remove-Item -Path ($Dir.FullName) -Recurse -Force -ErrorAction SilentlyContinue| Out-Null
       }
       foreach($File in $RootFileList){
           Remove-Item -Path ($File.FullName) -Recurse -Force -ErrorAction SilentlyContinue| Out-Null
       }
    }
    
    #  加载进度的显示
    for($counter = 0 ; $counter -lt 5 ; $counter++ ){
      $StrBlank="                         " #25
      $StrRate="ooooooooooooooooooooooooo" 
      $Process=($StrRate.Substring(0,$counter+1))+$StrBlank.Substring($counter+1)
      Write-Host (" "+"正在清理缓存文件"+"...["+$Process+"]")
      [Console]::SetCursorPosition(0, [Console]::CursorTop - 1)
      Start-Sleep -Milliseconds 2 ;
    }
    $escapeChar = [char]27
    Write-Host "${escapeChar}[9999C"
    #  加载进度的显示
    
     $DriectryList = Get-ChildItem -Path $CopyDir -Recurse -Directory -Depth 0
     $FileList = Get-ChildItem -Path $CopyDir -Recurse -File -Depth 0
     foreach ($File in $FileList) {
         Copy-Item -Path $File.FullName -Destination ($TargetDir) -Recurse -Force | Out-Null
     }
 
     foreach ($Directory in $DriectryList) {
        if(Test-Path -Path ($TargetDir+"\"+$Directory.Name)){
        <# 有当前文件夹再次遍历文件夹里的文件 #>
        $FileList = Get-ChildItem -Path $Directory.FullName -Recurse -File -Depth 0
        $DirList = Get-ChildItem -Path $Directory.FullName -Recurse -Directory -Depth 0

        # Read-Host ("文件夹 "+ $Directory.Name +" DirList的个数："+$DirList.Length)

        foreach ($File in $FileList) {
            $FilePath=$TargetDir+"\"+$Directory.Name+"\"+$File
            if(!(Test-Path -Path $FilePath)){
            <# 如果没有当前文件的话则复制过去 #>
            Copy-Item -Path $File.FullName -Destination ($TargetDir+"\"+$Directory.Name) -Recurse -Force | Out-Null
            }
        }
        <# 如果文件里面还有文件夹则递归调用 #>
        if($DirList.Length -ne 0){
            CopyFile $Directory.FullName ($TargetDir+"\"+$Directory.Name)
        }
        }else{
        <# 没有当前文件夹则复制 #>
        Copy-Item -Path $Directory.FullName -Destination $TargetDir -Recurse -Force | Out-Null
        }  
     }

    #  加载进度的显示
     [Console]::SetCursorPosition(0, [Console]::CursorTop-1)
     Start-Sleep -Milliseconds 2 ;
    for($counter = 0 ; $counter -lt 27 ; $counter++ ){
         $StrBlank="ooooo                    " #25
         $StrRate="ooooooooooooooooooooooooo"
            if($counter -lt $StrBlank.Length){
                $Process=($StrRate.Substring(0,$counter+1))+$StrBlank.Substring($counter+1)
                Write-Host (" "+"正在清理缓存文件"+"...["+$Process+"]")
            }else{
                 $StrRate="ooooooooooooooooooooooooo"
                Write-Host (" "+"正在清理缓存文件"+"...["+$Process+"] >>完成！") -ForegroundColor Green 
            }

            [Console]::SetCursorPosition(0, [Console]::CursorTop-1)
            Start-Sleep -Milliseconds 2 ;
    }
     $escapeChar = [char]27
     Write-Host "${escapeChar}[9999C"
    #  加载进度的显示
}


 # 判断当前项目是 清空还是追加
 if($Global:TestType -eq "SpringBootOpt" ){
    $InitMapperPath=".\src\main\java\com\yyge\mapper"
 }
 
 if($Global:TestType -eq "MavenWeb"){
    $InitMapperPath=".\src\main\java\com\yyge\mapper"  
}

if($Global:TestType -eq "SpringBoot" ){
    $InitMapperPath=".\src\main\java\com\yyge\mapper"
 }

if(Test-Path -Path $InitMapperPath){
    $FileArr= Get-ChildItem -Path $InitMapperPath -Recurse -File
    foreach ($File in $FileArr) {
        $NameStr=$File.Name -replace "Mapper.java",""
        if($Global:TestType -ne "MavenWeb"){
            $Path=".\src\main\java\com\yyge\controller\admin\"+($NameStr.ToLower())
            # Read-Host ($Path+(Test-Path -Path $Path))
            if(!(Test-Path -Path $Path)){
                if($global:AddMode -eq $false){
                    $global:AddMode=$true
                }
                
            }
        }

    }
}


if($Global:TestType -eq "MavenWeb"){
    InitRootFile "D:\我的编辑\PowerShell\前后端自动化Spring\.temp\InitRootFile\MawenWeb" ".\"  
}

if($Global:TestType -eq "SpringBootOpt"){
    # InitRootFile "D:\我的编辑\PowerShell\前后端自动化Spring\.temp\InitRootFile\SpringBootOpt" ".\"  
}

if($Global:TestType -eq "SpringBoot"){
    InitRootFile "D:\我的编辑\PowerShell\前后端自动化Spring\.temp\InitRootFile\SpringBoot" ".\"  
}



if($Global:Testing -eq $true){
    #自复制.temp文件
    Copy-Item -Path "D:\我的编辑\PowerShell\前后端自动化Spring\.temp" -Destination ".\" -Recurse -Force | Out-Null
}


# 取读配置文件
$ConfigTemplateBody=Get-Content -Path "./.temp/.config_" -Encoding utf8 -Raw
# Copy-Item -Path "./.temp/.config_" -Destination "./" -Recurse -Force | Out-Null
New-Item -ItemType File -Path "./.config" -Value $ConfigTemplateBody -Force | Out-Null
Show-Rate "正在取读配置文件"
Write-Host (" 取读完毕,按下任意键继续...")
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')

# 去除注释的正则
$pattern = "(?s)//.*?$|/\*.*?\*/"
$ConfigText= (Get-Content -Raw -Path "./.config"-Encoding utf8) -replace $pattern,""
$Global:ConfigData = ConvertFrom-Json $ConfigText